/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-04-20 11:28:05
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-05-19 17:37:22
 * @FilePath: \label_rack_control\firmware\bsp\bsp_usart.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "bsp_sys.h"
#include "bsp_usart.h"  
#include "apm32f10x_conf.h"    
#include "ui_screen.h"

uint8_t uart1_recv_data[USART_RECV_SIZE] = {0};
uint8_t uart3_recv_data[USART_RECV_SIZE] = {0};

// 重定义fputc函数
int fputc(int ch, FILE *f)
{
    USART_TxData(USART3, (uint8_t)ch);
    while (USART_ReadStatusFlag(USART3, USART_FLAG_TXC) == RESET)
        ;
    return ch;
}

/**
 * 初始化usart1 (与屏幕通信)
*/
void bsp_uart1_init(uint32_t bound)
{
    GPIO_Config_T GPIO_ConfigStruct;
    USART_Config_T USART_ConfigStruct;

    RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_USART1) | (RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));

    GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
    GPIO_ConfigStruct.pin = GPIO_PIN_9;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(GPIOA, &GPIO_ConfigStruct);

    GPIO_ConfigStruct.mode = GPIO_MODE_AF_OD;
    GPIO_ConfigStruct.pin = GPIO_PIN_10;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(GPIOA, &GPIO_ConfigStruct);

    USART_ConfigStruct.baudRate = bound;
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
    USART_ConfigStruct.mode = USART_MODE_TX_RX;
    USART_ConfigStruct.parity = USART_PARITY_NONE;
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
    USART_Config(USART1, &USART_ConfigStruct);

    USART_EnableInterrupt(USART1, USART_INT_IDLE);
    NVIC_EnableIRQRequest(USART1_IRQn, 2, 0);

    DMA_Config_T dmaConfig;
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);

    dmaConfig.peripheralBaseAddr = (USART1_BASE + 0x04);
    dmaConfig.memoryBaseAddr = (uint32_t)uart1_recv_data;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_SRC;
    dmaConfig.bufferSize = sizeof(uart1_recv_data);
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
    dmaConfig.loopMode = DMA_MODE_NORMAL;
    dmaConfig.priority = DMA_PRIORITY_MEDIUM;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;
    DMA_Config(DMA1_Channel5, &dmaConfig);
    DMA_Enable(DMA1_Channel5);
    USART_EnableDMA(USART1, USART_DMA_RX);

    USART_Enable(USART1);
}

/**
 * 串口1 发送数据
*/
void bsp_usart1_send_buffer(uint8_t* buff, uint8_t len)
{
    for(uint8_t i = 0; i < len; i++)
    {
        USART_TxData(USART1, buff[i]);
        while (USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
    }
}

/**
 * 串口1 接收中断
*/

void USART1_IRQHandler(void)    // 串口1 的中断处理函数
{
    
    ui_screen_t* ui_dev = ui_screen_dev();
    if(USART_ReadIntFlag(USART1, USART_INT_IDLE))
    {
        USART_ClearIntFlag(USART1, USART_INT_IDLE);
        DMA_Disable(DMA1_Channel5);
        uint8_t len = sizeof(uart1_recv_data) - DMA_ReadDataNumber(DMA1_Channel5);
        ring_buf_put(&ui_dev->ring_buff, uart1_recv_data, len);

        DMA_ConfigDataNumber(DMA1_Channel5, sizeof(uart1_recv_data));
        DMA_Enable(DMA1_Channel5);
    }
}


/**
 * 初始化usart3 (与屏幕通信)
*/
void bsp_uart3_init(uint32_t bound)
{
    GPIO_Config_T GPIO_ConfigStruct;
    USART_Config_T USART_ConfigStruct;

    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_USART3);
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);

    GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
    GPIO_ConfigStruct.pin = GPIO_PIN_10;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(GPIOB, &GPIO_ConfigStruct);

    GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
    GPIO_ConfigStruct.pin = GPIO_PIN_11;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(GPIOB, &GPIO_ConfigStruct);

    USART_ConfigStruct.baudRate = bound;
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
    USART_ConfigStruct.mode = USART_MODE_TX_RX;
    USART_ConfigStruct.parity = USART_PARITY_NONE;
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
    USART_Config(USART3, &USART_ConfigStruct);

    //USART_EnableInterrupt(USART3, USART_INT_IDLE);
    USART_EnableInterrupt(USART3, USART_INT_RXBNE);
    NVIC_EnableIRQRequest(USART3_IRQn, 2, 0);
#if 0
    DMA_Config_T dmaConfig;
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);

    dmaConfig.peripheralBaseAddr = (USART3_BASE + 0x04);
    dmaConfig.memoryBaseAddr = (uint32_t)uart3_recv_data;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_SRC;
    dmaConfig.bufferSize = sizeof(uart3_recv_data);
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
    dmaConfig.loopMode = DMA_MODE_NORMAL;
    dmaConfig.priority = DMA_PRIORITY_MEDIUM;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;
    DMA_Config(DMA1_Channel3, &dmaConfig);
    DMA_Enable(DMA1_Channel3);
    USART_EnableDMA(USART3, USART_DMA_RX);
#endif
    USART_Enable(USART3);
}

/**
 * 串口1 发送数据
*/
void bsp_usart3_send_buffer(uint8_t* buff, uint8_t len)
{
    for(uint8_t i = 0; i < len; i++)
    {
        USART_TxData(USART3, buff[i]);
        while (USART_ReadStatusFlag(USART3, USART_FLAG_TXC) == RESET);
    }
}

/**
 * 串口3 接收中断
*/

void USART3_IRQHandler(void)    // 串口3 的中断处理函数
{
    ui_screen_t* ui_dev = ui_screen_dev();
#if 0
    if(USART_ReadIntFlag(USART3, USART_INT_IDLE))
    {
        USART_ClearIntFlag(USART3, USART_INT_IDLE);
        DMA_Disable(DMA1_Channel3);
        uint8_t len = sizeof(uart3_recv_data) - DMA_ReadDataNumber(DMA1_Channel3);
        ring_buf_put(&ui_dev->ring_buff, uart3_recv_data, len);

        DMA_ConfigDataNumber(DMA1_Channel3, sizeof(uart3_recv_data));
        DMA_Enable(DMA1_Channel3);
    }
#endif
    if(USART_ReadIntFlag(USART3, USART_INT_RXBNE))
    {
        uint8_t dat = USART_RxData(USART3);
        ring_buf_put(&ui_dev->ring_buff, &dat, 1);
    }
}
